// key-value 模型
public class HashBucket {
    private static class Node {
        private int key;
        private int value;
        Node next;


        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }


    private Node[] array = new Node[10];
    private int size;   // 当前的数据个数
    private static final double LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 8;//默认桶的大小

    public void put(int key, int value) {
        // write code here
        int index = key % array.length;
        //遍历一遍若有相同key则更新
        Node cur = array[index];
        while(cur!=null){
            if(cur.key == key){
                cur.key = value;
                return;
            }
        }
        //插入该节点
        Node node = new Node(key,value);
        node.next = array[index];
        array[index] = node;
        size++;
        //判断是否扩容
        if(loadFactor()>LOAD_FACTOR){
            resize();
        }
    }


    private void resize() {
        // write code here
        Node[] newArray = new Node[2*array.length];
        for(int i =0;i<array.length-1;i++){
            Node cur = array[i];
            while(cur!=null){
                int index = cur.key/newArray.length;
                Node curN = cur.next;
                curN = newArray[index];
                cur = newArray[index];
                cur = curN;
            }

        }
        array = newArray;
    }


    private double loadFactor() {
        return size * 1.0 / array.length;
    }

    public int getVal(int key){
        int index = key%array.length;
        Node cur =array[index];
        while(cur!= null){
            if(cur.key ==key){
                return cur.value;
            }
            cur = cur.next;
        }
        return -1;
    }
}